﻿using HelenServer.Dapr.DataProtection;
using HelenServer.Data.SqlServer;
using HelenServer.UserCenter.Dal;
using HelenServer.UserCenter.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

var builder = WebApplication.CreateBuilder(args);

builder.AddCloudService();
builder.AddCloudDaprService();


builder.Services.AddAutoMapper<UserCenterProfile>();
builder.Services.AddAutoMapper<UserCenterMigrateProfile>();
builder.Services.AddDbContextPool<UserCenterDbContext>(optsBuilder =>
{
    optsBuilder.LogTo(n => Console.WriteLine(n), LogLevel.Warning, DbContextLoggerOptions.DefaultWithLocalTime).EnableSensitiveDataLogging().UseLazyLoadingProxies().UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll).UseSqlServer(builder.Configuration.GetConnectionString("master"));
});
builder.Services.AddSingleton<IReadWriteSplittingDbContextFactory<UserCenterDbContext>, ReadWriteSplittingDbContextFactory<UserCenterDbContext>>();

builder.Services.AddDataProtection().PersistKeysToDaprState();
builder.Services.AddCloudIdentityServer(builder.Configuration);
builder.Services.AddNonBreakingSameSiteCookies();
builder.Services.AddCors(opts => builder.Configuration.Bind(opts));

builder.Services.AddSeq<DalUserService>();
builder.Services.AddSeq<UCMigrateSingal>();

var app = builder.Build();

Helen.Services = app.Services;

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCloudService();
app.UseCloudDaprService();

app.UseCors();
app.UseCookiePolicy();

app.UseCloudIdentityServer();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();